﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Browser;
using System.Collections.Generic;

using SilverApp.View;

namespace SilverApp.ViewModel 
{
    public class MainPageViewModel : ViewModelBase
    {
        #region private members
        
        private UIElement           currentPage;
        private bool                connected = false;
        private LiveService.User    currentUser = null;
        private string               message = "no message";

        private ICommand launchregisterCommand;
        private ICommand launchauthCommand;
        AuthView authView = new AuthView();
        private RegisterWindow regWin;
        private AuthWindow authWin;
        #endregion

        public MainPageViewModel()
        {
            launchregisterCommand = new RelayCommand(LaunchRegister, CanLaunchRegister);
            launchauthCommand = new RelayCommand(LaunchAuth, CanLaunchAuth);
            this.RegisterNextView("Welcome");
        }

        public UIElement CurrentPage
        {
            get { return currentPage; }
            set
            {
                currentPage = value;
                OnPropertyChanged("CurrentPage");
            }
        }

        public LiveService.User CurrentUser
        {
            get { return currentUser; }
            set { this.currentUser = value; }
        }

        public string Message
        {
            get { return this.message; }
            set { this.message = value; OnPropertyChanged("Message"); }
        }

        public RegisterWindow RegWin
        {
            get { return this.regWin; }
            set { this.regWin = value; }
        }

        public AuthWindow AuthWin
        {
            get { return this.authWin; }
            set { this.authWin = value; }
        }

        public void RegisterNextView(String next)
        {
            switch (next)
            {
                case "Register" :
                    //CurrentPage = new RegisterView(new RegisterViewModel(this));
                    regWin = new RegisterWindow();
                    regWin.LayoutRoot.Children.Add(new RegisterView(new RegisterViewModel(this)));
                    regWin.Show();
                    break;

                case "Auth":
                    authWin = new AuthWindow();
                    authWin.LayoutRoot.Children.Add(new AuthView(new AuthViewModel(this)));
                    authWin.Show();
                    break;

                case "Welcome":
                    CurrentPage = new WelcomeView(this);
                    break;

                case "Home":
                    CurrentPage = new HomeView(new HomeViewModel(this));
                    break;

                default:
                    CurrentPage = new AuthView(new AuthViewModel(this));
                    break;
            }
        }

        public void RegisterUser(LiveService.User cuser)
        {
            this.currentUser = cuser;
        }

        public String WelcomeText
        {
            get { return "Welcome on your My Live Mesh Application.\n" +
                         "\nUpload, download and synchronize all your files." +
                         "Share it with your friends, share it in your computers at home or at work. " +
                         "Click on login to connect or register in you have not yet your own free account !"; }
        }



        public ICommand LaunchregisterCommand
        {
            get { return this.launchregisterCommand; }
            set { this.launchregisterCommand = value; }
        }

        public void LaunchRegister(object parameter)
        {
            try
            {
                this.RegisterNextView("Register");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        public bool CanLaunchRegister(object parameter)
        {
            return true;
        }

        public ICommand LaunchAuthCommand
        {
            get { return this.launchauthCommand; }
            set { this.launchauthCommand = value; }
        }

        public void LaunchAuth(object parameter)
        {
            try
            {
                this.RegisterNextView("Auth");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        public bool CanLaunchAuth(object parameter)
        {
            return true;
        }
    }
}
